package com.ifacebox.web.admin.master.controller;

import com.ifacebox.web.admin.controller.BaseBodyController;
import com.ifacebox.web.admin.master.model.MasterNotice;
import com.ifacebox.web.admin.master.model.MasterNoticeQuery;
import com.ifacebox.web.admin.master.model.MasterUser;
import com.ifacebox.web.admin.master.model.MasterUserNotice;
import com.ifacebox.web.admin.master.service.MasterNoticeService;
import com.ifacebox.web.admin.master.service.MasterUserService;
import com.ifacebox.web.common.annotation.UserAgentRequest;
import com.ifacebox.web.common.model.BaseResponse;
import com.ifacebox.web.common.model.ModelResponse;
import com.ifacebox.web.common.model.StatusResponse;
import com.ifacebox.web.common.model.UserAgent;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author znn
 */
@Controller
public class MasterNoticeController extends BaseBodyController {
    @Resource
    private MasterNoticeService masterNoticeService;

    @GetMapping("/admin/notice/list.json")
    @ResponseBody
    public ModelResponse<List<MasterNotice>> list(@UserAgentRequest UserAgent userAgent, MasterNoticeQuery masterNoticeQuery) {
        if (!permissionManager.hasLogin(userAgent)) {
            return new ModelResponse<>(StatusResponse.LOGOUT);
        }
        MasterUser masterUser = userAgent.getMasterUser();
        masterNoticeQuery.setUserId(masterUser.getId());
        if (!MasterUserService.ADMIN_NAME.equals(masterUser.getUsername())) {
            masterNoticeQuery.setCreateUser(masterUser.getUsername());
        }
        return new ModelResponse<>(StatusResponse.SUCCESS, masterNoticeService.total(masterNoticeQuery), masterNoticeService.query(masterNoticeQuery));
    }

    @GetMapping("/admin/notice/show.json")
    @ResponseBody
    public ModelResponse<MasterNotice> show(@UserAgentRequest UserAgent userAgent, @RequestParam Integer id) {
        if (!permissionManager.hasLogin(userAgent)) {
            return new ModelResponse<>(StatusResponse.LOGOUT);
        }
        MasterNotice masterNotice = masterNoticeService.queryById(id);
        if (ObjectUtils.isEmpty(masterNotice)) {
            return new ModelResponse<>(StatusResponse.DELETE_FAIL, "通知不存在！");
        }
        MasterUserNotice masterUserNotice = new MasterUserNotice();
        masterUserNotice.setUserId(userAgent.getMasterUser().getId());
        masterUserNotice.setNoticeId(masterNotice.getId());
        masterNoticeService.save(masterUserNotice);
        masterNotice.setReadMasterUsers(masterNoticeService.queryReadMasterUserByNoticeId(id));
        return new ModelResponse<>(StatusResponse.SUCCESS, masterNotice);
    }

    @PostMapping("/admin/notice/save.json")
    @ResponseBody
    public BaseResponse save(@UserAgentRequest UserAgent userAgent, @RequestBody @Validated MasterNotice masterNotice) {
        if (!permissionManager.hasPermission(userAgent)) {
            return new ModelResponse<>(StatusResponse.LOGOUT);
        }
        if (!StringUtils.isEmpty(masterNotice.getReceiveUser())) {
            MasterUser masterUser = masterUserService.queryByUsernameOrMobileOrEmail(masterNotice.getReceiveUser());
            if (ObjectUtils.isEmpty(masterUser)) {
                return new BaseResponse(StatusResponse.SAVE_FAIL, "找不到要发送通知的用户！");
            }
            masterNotice.setReceiveUser(masterUser.getUsername());
        }
        MasterUser masterUser = userAgent.getMasterUser();
        if (!masterNoticeService.save(masterUser, masterNotice)) {
            return new BaseResponse(StatusResponse.SAVE_FAIL);
        }
        traceLoggerService.addTraceLogger(userAgent, "发送通知成功！【{}】", masterNotice.toString());
        return new BaseResponse(StatusResponse.SUCCESS);
    }

    @PutMapping("/admin/notice/edit.json")
    @ResponseBody
    public BaseResponse edit(@UserAgentRequest UserAgent userAgent, @RequestBody @Validated MasterNotice masterNotice) {
        if (!permissionManager.hasPermission(userAgent)) {
            return new ModelResponse<>(StatusResponse.LOGOUT);
        }
        MasterUser masterUser = userAgent.getMasterUser();
        MasterNotice oldMasterNotice = masterNoticeService.queryById(masterNotice.getId());
        if (ObjectUtils.isEmpty(oldMasterNotice)) {
            return new BaseResponse(StatusResponse.UPDATE_FAIL, "修改的通知不存在！");
        }
        if (!MasterUserService.ADMIN_NAME.equals(masterUser.getUsername())
                && !oldMasterNotice.getCreateUser().equals(masterUser.getUsername())) {
            return new BaseResponse(StatusResponse.UPDATE_FAIL, "你不能修改他人发布的通知！");
        }
        if (!StringUtils.isEmpty(masterNotice.getReceiveUser())) {
            MasterUser mMasterUser = masterUserService.queryByUsernameOrMobileOrEmail(masterNotice.getReceiveUser());
            if (ObjectUtils.isEmpty(mMasterUser)) {
                return new BaseResponse(StatusResponse.SAVE_FAIL, "找不到要修改通知的用户！");
            }
            masterNotice.setReceiveUser(mMasterUser.getUsername());
        }
        if (!masterNoticeService.edit(masterUser, masterNotice)) {
            return new BaseResponse(StatusResponse.UPDATE_FAIL);
        }
        traceLoggerService.addTraceLogger(userAgent, "修改通知成功！【{}】", masterNotice.toString());
        return new BaseResponse(StatusResponse.SUCCESS);
    }

    @DeleteMapping("/admin/notice/delete.json")
    @ResponseBody
    public BaseResponse delete(@UserAgentRequest UserAgent userAgent, @RequestParam Integer id) {
        if (!permissionManager.hasPermission(userAgent)) {
            return new ModelResponse<>(StatusResponse.LOGOUT);
        }
        MasterUser masterUser = userAgent.getMasterUser();
        MasterNotice masterNotice = masterNoticeService.queryById(id);
        if (ObjectUtils.isEmpty(masterNotice)) {
            return new BaseResponse(StatusResponse.DELETE_FAIL, "删除的通知不存在！");
        }
        if (!MasterUserService.ADMIN_NAME.equals(masterUser.getUsername())
                && !masterNotice.getReceiveUser().equals(masterUser.getUsername())
                && !masterNotice.getCreateUser().equals(masterUser.getUsername())) {
            return new BaseResponse(StatusResponse.UPDATE_FAIL, "只能删除自己发送或接收的通知！");
        }
        if (!masterNoticeService.delete(id)) {
            return new BaseResponse(StatusResponse.UPDATE_FAIL);
        }
        traceLoggerService.addTraceLogger(userAgent, "删除通知成功！【{}】", masterNotice.toString());
        return new BaseResponse(StatusResponse.SUCCESS);
    }
}
